package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    //扩展方法 ,根据id删除分类,检查菜品,套餐关联关系
    @Override
    public void deleteById(Long id) {
        //根据分类id查询菜品
        Integer count1 = dishService.lambdaQuery()
                .eq(Dish::getCategoryId, id)
                .count();
        if (count1>0){
            throw new CustomerException("该类不能删除,有菜品关联数据");
        }

        //根据分类id去查询套餐
        Integer count2 = setmealService.lambdaQuery()
                .eq(Setmeal::getCategoryId, id)
                .count();
        if (count2>0){
            throw new CustomerException("该类不能删除,有套餐关联数据");
        }

        //根据分类Id删除数据  this表示本类对象
        this.removeById(id);
    }




}
